package org.sdnstproject.interlayer.OF13Protocal;

import java.util.HashMap;

import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuHello {

	private static final Logger log = LoggerFactory.getLogger(FuHello.class);
    /**version 类型:byte 默认值: 1.3*/
    public byte version = 4;
    /**type 类型:byte 默认值:0x0*/
    public byte type = 0x0;
    /**length 类型:short 默认值:8*/
    public short length = 8;
    /**xid 类型:long 默认值: 0x0L*/
    public long xid = 0x0L;
    
    public FuHello() {
    	
    }
    
    public FuHello setXid(long xid) {
		this.xid = xid;
		return this;
	}
    
    /**将FuHello对象写入ByteBuf*/
	public FuHello(ByteBuf bb) {
		// fixed value property version == 4
        this.version = bb.readByte();
        // fixed value property type
        this.type = bb.readByte();
        if(type != (byte) 0x0)
            log.info("Wrong type: Expected=OFType.HELLO(0), got="+type);
        this.length = bb.readShort();
        this.xid = U32.f(bb.readInt());
    }
    
    /**将FuHello对象写入ByteBuf*/
	public void writeTo(ByteBuf bb) {
        // fixed value property version 
        bb.writeByte(this.version);
        // fixed value property type 
        bb.writeByte((byte) 0x0);
        // fixed value property length
        bb.writeShort(this.length);
        bb.writeInt(U32.t(this.xid));
    }
	
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
        // fixed value property version = 4
        bb.writeByte(this.version);
        // fixed value property type = 12
        bb.writeByte((byte) 0xc);
        // fixed value property length = 80
        bb.writeShort(this.length);
        bb.writeInt(U32.t(this.xid));
        return bb;
    }

	public FuHello setField(String field, String value) {
		switch (field) {
		case "version":
			this.setVersion(Byte.parseByte(value));
			break;
		case "type":
			this.setType(Byte.parseByte(value));
			break;
		case "length":
			this.setLength(Short.parseShort(value));
			break;
		case "xid":
			this.setXid(Long.parseLong(value));
			break;
		}
		return this;
	}
	
	public FuHello setVersion(byte version) {
		this.version = version;
		return this;
	}

	public FuHello setType(byte type) {
		this.type = type;
		return this;
	}

	public FuHello setLength(short length) {
		this.length = length;
		return this;
	}
	
	/**记录FuHello字段和其类型映射*/
    public HashMap<String, String> FuHelloFields = new HashMap<String, String>() {
		{
			put("version","byte");
			put("type","byte");
			put("length","short");
			put("xid","long");
		}
    };
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFHelloVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(")");
        return b.toString();
    }

}
